﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using EbbinghausWord.BLL;
using System.Linq;

namespace EbbinghausWord.Entities
{
    /// <summary>
    /// 单词实体类
    /// </summary>
    public class WordEntity : INotifyPropertyChanged, IEquatable<WordEntity>
    {
        #region public property
        /// <summary>
        /// 英语
        /// </summary>
        public string English { get; set; }

        private int _rememberLevel;
        /// <summary>
        /// 记忆级别
        /// </summary>
        public int RememberLevel
        {
            get { return _rememberLevel; }
            set
            {
                _rememberLevel = value;
                NotifyChanged("RememberLevel");
            }
        }

        /// <summary>
        /// 记忆次数，记住3次则算做记住
        /// </summary>
        public int RememberTimes { get; set; }

        /// <summary>
        /// 开始记忆的时间
        /// </summary>
        public DateTime StartRemeberingDateTime { get; set; }

        /// <summary>
        /// 最后记忆的时间
        /// </summary>
        public DateTime LastRemeberingDateTime { get; set; }

        /// <summary>
        /// 是否在单词本中
        /// </summary>
        public bool IsInTheWordBook { get; set; }

        /// <summary>
        /// 记忆计数器
        /// </summary>
        public int RememberCounter { get; set; }

        /// <summary>
        /// 未记住的计数器
        /// </summary>
        public int NotRememberCounter { get; set; }


        private int _orderRandom = 0;
        /// <summary>
        /// 随机排序的种子
        /// </summary>
        public int OrderRandom
        {
            get
            {
                if (_orderRandom == 0)
                {
                    var hashBytes = _hash.ComputeHash(Encoding.UTF8.GetBytes(English));
                    _orderRandom = (int)hashBytes[0] + (int)hashBytes[1];
                    //var strs = hashBytes.Select(x => Convert.ToString(x, 16));
                    //_orderRandom = string.Join("", strs);
                }

                return _orderRandom;
            }
        }

        #endregion

        #region 词典中获取的属性，异步加载

        private DictionaryWord _dictionaryWord = null;

        /// <summary>
        /// 音标
        /// </summary>
        public string Soundmark
        {
            get { return GetDictionaryWord().Soundmark; }
        }

        /// <summary>
        /// 中文
        /// </summary>
        public string Chinese
        {
            get
            {
                return GetDictionaryWord().Chinese;
            }
        }

        /// <summary>
        /// 同义词
        /// </summary>
        public List<SynonymEntity> Synonyms
        {
            get { return GetDictionaryWord().Synonyms; }
        }

        /// <summary>
        /// 词组
        /// </summary>
        public List<WordGroupEntity> WordGroups
        {
            get { return GetDictionaryWord().WordGroups; }
        }

        /// <summary>
        /// 例句
        /// </summary>
        public List<SentenceEntity> Sentences
        {
            get
            {
#if FREE
                return GetDictionaryWord().Sentences.Take(1).ToList();
#else
                return SystemConfig.IsTrial ? GetDictionaryWord().Sentences.Take(1).ToList() : GetDictionaryWord().Sentences;
#endif
            }
        }

        #endregion

        #region private field
        private static HashAlgorithm _hash = new SHA1Managed();
        #endregion

        #region private method

        /// <summary>
        /// 延迟加载词典单词
        /// </summary>
        private DictionaryWord GetDictionaryWord()
        {
            //return null;
            return _dictionaryWord ?? (_dictionaryWord = DictionaryManager.GetWord(English));
        }

        #endregion

        #region public method

        /// <summary>
        /// 属性变化事件
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 通知属性变化
        /// </summary>
        /// <param name="property"></param>
        private void NotifyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public bool Equals(WordEntity other)
        {
            return other.English == English;
        }

        /// <summary>
        /// 转换成字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6}", English, IsInTheWordBook ? 1 : 0,
                RememberLevel, StartRemeberingDateTime, RememberCounter, NotRememberCounter, RememberTimes);
        }

        /// <summary>
        /// 从字符串生成单词实体
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static WordEntity CreateEntityFromString(string str)
        {
            var entities = str.Split(',');
            return new WordEntity()
                                 {
                                     English = entities[0].Trim(),
                                     IsInTheWordBook = entities[1] == "1",
                                     RememberLevel = int.Parse(entities[2]),
                                     StartRemeberingDateTime = DateTime.Parse(entities[3]),
                                     RememberCounter = int.Parse(entities[4]),
                                     NotRememberCounter = int.Parse(entities[5]),
                                     RememberTimes = entities.Length == 6 ? 0 : int.Parse(entities[6]),
                                 };
        }

        /// <summary>
        /// 从数据实体转换
        /// </summary>
        /// <param name="tableUserWordEntity"></param>
        /// <returns></returns>
        public static WordEntity FromTable(TableUserWordEntity tableUserWordEntity)
        {
            return new WordEntity()
                       {
                           English = tableUserWordEntity.English,
                           IsInTheWordBook = tableUserWordEntity.IsInTheWordBook,
                           NotRememberCounter = tableUserWordEntity.NotRememberCounter,
                           RememberCounter = tableUserWordEntity.RememberCounter,
                           RememberLevel = tableUserWordEntity.RememberLevel,
                           RememberTimes = tableUserWordEntity.RememberTimes,
                           StartRemeberingDateTime = tableUserWordEntity.StartRemeberingDateTime.Value,
                           LastRemeberingDateTime = tableUserWordEntity.LastRemeberingDateTime.Value,
                           //OrderRandom = tableUserWordEntity.OrderRandom,
                       };
        }

        /// <summary>
        /// 转换成数据实体
        /// </summary>
        /// <param name="tableUserWordEntity"></param>
        /// <returns></returns>
        public TableUserWordEntity ToTable()
        {
            return new TableUserWordEntity()
            {
                English = this.English,
                IsInTheWordBook = this.IsInTheWordBook,
                NotRememberCounter = this.NotRememberCounter,
                RememberCounter = this.RememberCounter,
                RememberLevel = this.RememberLevel,
                RememberTimes = this.RememberTimes,
                StartRemeberingDateTime = this.StartRemeberingDateTime,
                LastRemeberingDateTime = this.LastRemeberingDateTime,
                //OrderRandom = this.OrderRandom,
            };
        }

        #endregion

        #region 显示属性

        public string WordStatus
        {
            get
            {
                if (!IsInTheWordBook)
                {
                    return "生词";
                }
                return RememberLevel == SystemConfig.AchievementRememberLevel ? "熟词" : "学习中";
            }
        }

        #endregion
    }
}
